﻿using FieldTools.Client.Entity.MyQQ;
using FieldTools.Util;
using FieldTools.Util.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FieldTools.Client.Entity.Model
{
    /// <summary>
    /// 自定义脚本类
    /// </summary>
    public class CustomScript
    {
        /// <summary>
        /// 一个雪花id的构造器
        /// </summary>
        public readonly static IdHelper idHelper = new();
        private string id = idHelper.NextId().ToString();
        /// <summary>
        /// 主键
        /// </summary>
        public string Id
        {
            get { return id; }
            set { id = value; }
        }

        private ScriptType scriptType;
        /// <summary>
        /// 脚本类型
        /// </summary>
        public ScriptType ScriptType
        {
            get { return scriptType; }
            set { scriptType = value; }
        }

        private object parm = null;
        /// <summary>
        /// 参数
        /// </summary>
        public object Parm
        {
            get { return parm; }
            set { parm = value; }
        }

        private string scriptContent = string.Empty;
        /// <summary>
        /// 脚本内容
        /// </summary>
        public string ScriptContent
        {
            get { return scriptContent; }
            set { scriptContent = value; }
        }

        /// <summary>
		/// 执行脚本
		/// </summary>
		/// <returns></returns>
		public string? Excute()
        {
            if (string.IsNullOrEmpty(ScriptContent)) return "可执行脚本为空";
            switch (ScriptType)
            {
                case ScriptType.纯文本:
                    return ScriptContent;
                case ScriptType.CShape脚本:
                    return CShapeScriptHepler.ExcuteScript(ScriptContent, Parm);
                case ScriptType.JS脚本:
                    return JSHelper.RunJsFromStr(ScriptContent, null, "excute", [Parm]).Result;
                case ScriptType.Python脚本:
                    return PythonHelper.ExcuteScriptByStr(ScriptContent, Parm);
                case ScriptType.插值:
                    break;
                default:
                    break;
            }
            return ScriptContent;
        }
    }

    /// <summary>
    /// 脚本类型
    /// </summary>
    public enum ScriptType
    {
        /// <summary>
        /// 纯文本，保持原来的值不变
        /// </summary>
        纯文本 = 0,
        /// <summary>
        /// 执行一段c#脚本，脚本必须要定义“public void Excute()”作为入口
        /// </summary>
        CShape脚本 = 1,
        /// <summary>
        /// 执行一段js脚本，脚本必须要定义“Excute()”作为入口
        /// </summary>
        JS脚本 = 2,
        /// <summary>
        /// 执行一段python脚本，todo
        /// </summary>
        Python脚本 = 3,
        /// <summary>
        /// 插值模式，todo
        /// </summary>
        插值 = 4
    }
}
